using BasicScript.Component;
using Game.Const;
using Game.Core;
using Game.Main.Model;
using Game.Main.View;
using Game.Manager;
using Game.XH;
using SimpleJSON;
using UnityEngine;

namespace Game.Component
{
	public class MedicineMachineItem : BaseItem
	{
		private enum Anim
		{
			Static = 0,
			Running = 1
		}

		private const float BAR_NORMAL_FILL = 0.7f;

		private const float BAR_MIN_FILL = 0.4f;

		private const float BAR_MAX_FILL = 1f;

		private const float TUBE_NORMAL_FILL = 0.45f;

		private const int MIN_ANGLE = -60;

		private const int MAX_ANGLE = 60;

		private const int MIN_NUM = 0;

		private const int MAX_NUM = 999;

		private Anim anim;

		private float timer;

		private int stage;

		private BaseItem targetItem;

		private SpriteRenderer bottleWater;

		private SpriteRenderer tubeWater;

		private SpriteRenderer redWater;

		private SpriteRenderer blueWater;

		private float bottleFillAmount;

		private float tubeFillAmount;

		private float redFillAmount;

		private float blueFillAmount;

		private Transform bottle;

		private float modelOriginalX;

		private float modelOriginalY;

		private Transform pointer1;

		private float curPointer1Angle;

		private float pointer1AddVal = 90f;

		private float wait1Timer;

		private float pointer1Timer;

		private bool pointer1MoveLeft;

		private Transform pointer2;

		private float curPointer2Angle;

		private float pointer2AddVal = 90f;

		private float wait2Timer;

		private float pointer2Timer;

		private bool pointer2MoveLeft;

		private int colorIndex;

		private Color32[] colors = new Color32[6]
		{
			new Color32(227, 81, 82, byte.MaxValue),
			new Color32(byte.MaxValue, 151, 57, byte.MaxValue),
			new Color32(197, 176, byte.MaxValue, byte.MaxValue),
			new Color32(45, 170, 173, byte.MaxValue),
			new Color32(byte.MaxValue, 223, 87, byte.MaxValue),
			new Color32(82, 220, 180, byte.MaxValue)
		};

		private Transform numTrans;

		private int num;

		private SpriteRenderer[] numRenders;

		private SpriteAssets spriteAssets;

		private float numTimer;

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
			bottle = model.Find("Bottle");
			bottle.gameObject.layer = 15;
			bottleWater = model.Find("Bottle/WaterFill").GetComponent<SpriteRenderer>();
			InitWaterFill(bottleWater);
			tubeWater = model.Find("Tube/WaterFill").GetComponent<SpriteRenderer>();
			InitWaterFill(tubeWater);
			redWater = model.Find("RedBar/WaterFill").GetComponent<SpriteRenderer>();
			InitWaterFill(redWater);
			blueWater = model.Find("BlueBar/WaterFill").GetComponent<SpriteRenderer>();
			InitWaterFill(blueWater);
			SetBottleFillAmount(1f);
			SetTubeFillAmount(0f);
			SetRedFillAmount(0.7f);
			SetBlueFillAmount(0.7f);
			pointer1 = model.Find("ClockA/Pointer");
			pointer2 = model.Find("ClockB/Pointer");
			numTrans = frontMask.Find("Num");
			spriteAssets = model.GetComponent<SpriteAssets>();
			modelOriginalX = model.localPosition.x;
			modelOriginalY = model.localPosition.y;
			SetColor(data.state);
		}

		private void SetColor(int index)
		{
			if (index < 0 || index >= colors.Length)
			{
				index = 0;
			}
			colorIndex = index;
			bottleWater.color = colors[colorIndex];
			tubeWater.color = colors[colorIndex];
		}

		private void InitWaterFill(SpriteRenderer spriteRender)
		{
			float num = spriteRender.sprite.texture.height;
			Rect textureRect = spriteRender.sprite.textureRect;
			float num2 = textureRect.yMin / num;
			float value = textureRect.yMax / num - num2;
			spriteRender.material.SetFloat("_StartY", num2);
			spriteRender.material.SetFloat("_TotalY", value);
		}

		private void SetBottleFillAmount(float val)
		{
			bottleFillAmount = val;
			bottleWater.material.SetFloat("_Fill", bottleFillAmount);
		}

		private void SetTubeFillAmount(float val)
		{
			tubeFillAmount = val;
			tubeWater.material.SetFloat("_Fill", 0.45f + tubeFillAmount * 0.55f);
		}

		private void SetRedFillAmount(float val)
		{
			redFillAmount = val;
			redWater.material.SetFloat("_Fill", redFillAmount);
		}

		private void SetBlueFillAmount(float val)
		{
			blueFillAmount = val;
			blueWater.material.SetFloat("_Fill", blueFillAmount);
		}

		protected override void LateUpdate()
		{
			base.LateUpdate();
			if (anim != Anim.Running)
			{
				return;
			}
			if (stage < 2)
			{
				model.SetLocalXY(modelOriginalX + Random.Range(-0.02f, 0.02f), modelOriginalY + Random.Range(-0.02f, 0.02f));
			}
			if (stage == 0)
			{
				timer += Time.deltaTime;
				SetBottleFillAmount(1f - timer);
				SetTubeFillAmount(timer);
				if (timer >= 1f)
				{
					stage = 1;
					timer = 0f;
				}
				numTimer += Time.deltaTime;
				if (numTimer > 0.2f)
				{
					numTimer = 0f;
					SetNumber(num + Random.Range(-10, 11), numTrans);
				}
			}
			else if (stage == 1)
			{
				timer += Time.deltaTime / 3f;
				if (targetItem != null)
				{
					targetItem.SetAlpha(1f - timer);
				}
				SetTubeFillAmount(1f - timer);
				pointer1Timer += Time.deltaTime;
				if (pointer1Timer > wait1Timer)
				{
					pointer1Timer = 0f;
					wait1Timer = GetRandomTime();
					pointer1MoveLeft = !pointer1MoveLeft;
				}
				if (pointer1MoveLeft)
				{
					SetBlueFillAmount(Mathf.Clamp(blueFillAmount + Time.deltaTime / 2f, 0.4f, 1f));
					SetPointer1Angle(Mathf.Clamp(curPointer1Angle - pointer1AddVal * Time.deltaTime, -60f, 60f));
				}
				else
				{
					SetBlueFillAmount(Mathf.Clamp(blueFillAmount - Time.deltaTime / 2f, 0.4f, 1f));
					SetPointer1Angle(Mathf.Clamp(curPointer1Angle + pointer1AddVal * Time.deltaTime, -60f, 60f));
				}
				pointer2Timer += Time.deltaTime;
				if (pointer2Timer > wait2Timer)
				{
					pointer2Timer = 0f;
					wait2Timer = GetRandomTime();
					pointer2MoveLeft = !pointer2MoveLeft;
				}
				if (pointer2MoveLeft)
				{
					SetRedFillAmount(Mathf.Clamp(redFillAmount + Time.deltaTime / 2f, 0.4f, 1f));
					SetPointer2Angle(Mathf.Clamp(curPointer2Angle - pointer2AddVal * Time.deltaTime, -60f, 60f));
				}
				else
				{
					SetRedFillAmount(Mathf.Clamp(redFillAmount - Time.deltaTime / 2f, 0.4f, 1f));
					SetPointer2Angle(Mathf.Clamp(curPointer2Angle + pointer2AddVal * Time.deltaTime, -60f, 60f));
				}
				numTimer += Time.deltaTime;
				if (numTimer > 0.2f)
				{
					numTimer = 0f;
					SetNumber(num + Random.Range(-10, 11), numTrans);
				}
				if (!(timer >= 1f))
				{
					return;
				}
				if (targetItem != null && !string.IsNullOrEmpty(targetItem.itemData.changeId))
				{
					string changeId = targetItem.itemData.changeId;
					GlobalEventSystem.Fire(new BaseEvent("game.remove_item", targetItem.archiveData.index));
					Transform transform = model.Find("CreatePoint");
					BaseItem baseItem = Singleton<Scene>.Instance.CreateItem(changeId, transform.position, 0);
					if (baseItem != null)
					{
						baseItem.DropDown(model.Find("CheckPoint").position, true);
						Singleton<AudioManager>.Instance.PlaySound("sound_explode");
						Singleton<EffectManager>.Instance.ShowEffect(PathConst.EFFECT + "efc_explode_1", new Vector3(baseItem.transform.position.x, baseItem.transform.position.y - 0.2f, base.transform.position.z - 0.002f), 1f);
						Singleton<EffectManager>.Instance.ShowEffect(PathConst.EFFECT + "efc_blink_1", new Vector3(baseItem.transform.position.x, baseItem.transform.position.y - 0.5f, base.transform.position.z - 0.002f), 3f);
					}
				}
				SetNumber(0, numTrans);
				stage = 2;
				timer = 0f;
				model.SetLocalXY(modelOriginalX, modelOriginalY);
				StopDragEffect();
				RemoveAudioPlayer();
			}
			else if (stage == 2)
			{
				timer += Time.deltaTime;
				SetBottleFillAmount(timer);
				SetBlueFillAmount(Mathf.MoveTowards(blueFillAmount, 0.7f, Time.deltaTime / 2f));
				SetPointer1Angle(Mathf.MoveTowards(curPointer2Angle, 0f, Time.deltaTime * 40f));
				SetRedFillAmount(Mathf.MoveTowards(redFillAmount, 0.7f, Time.deltaTime / 2f));
				SetPointer2Angle(Mathf.MoveTowards(curPointer2Angle, 0f, Time.deltaTime * 40f));
				if (timer >= 1f)
				{
					anim = Anim.Static;
				}
			}
		}

		public override bool AttachItem(BaseItem item, DepthType depthType, AttachType attachType, bool updatePosition)
		{
			bool flag = base.AttachItem(item, depthType, attachType, updatePosition);
			if (flag && item.itemData.placeType == 9)
			{
				StartRunning(item);
			}
			return flag;
		}

		public override bool CanPlaceItem(BaseItem item)
		{
			if (anim == Anim.Running)
			{
				return false;
			}
			return base.CanPlaceItem(item);
		}

		private void SetPointer1Angle(float val)
		{
			curPointer1Angle = val;
			pointer1.SetLocalRotationZ(curPointer1Angle);
		}

		private void SetPointer2Angle(float val)
		{
			curPointer2Angle = val;
			pointer2.SetLocalRotationZ(curPointer2Angle);
		}

		public override void TouchDown(TouchEventData touchEventData)
		{
			base.TouchDown(touchEventData);
			RaycastHit2D raycastHit2D = Physics2D.Raycast(touchEventData.curPos, Vector2.zero, 15f, 32768);
			if (raycastHit2D.collider != null && raycastHit2D.collider.transform == bottle)
			{
				Singleton<AudioManager>.Instance.PlaySound("sound_btn_1");
				if (anim == Anim.Static)
				{
					SetColor(colorIndex + 1);
				}
			}
		}

		private void StartRunning(BaseItem item)
		{
			if (anim == Anim.Static)
			{
				PlayDragEffect();
				anim = Anim.Running;
				timer = 0f;
				stage = 0;
				targetItem = item;
				targetItem.SetAllCollidersEnabled(false);
				SetBottleFillAmount(1f);
				SetTubeFillAmount(0.45f);
				SetRedFillAmount(0.7f);
				SetBlueFillAmount(0.7f);
				pointer1Timer = 0f;
				wait1Timer = GetRandomTime();
				pointer1MoveLeft = false;
				pointer2Timer = 0f;
				wait2Timer = GetRandomTime();
				pointer2MoveLeft = true;
				SetNumber(Random.Range(0, 999), numTrans);
				numTimer = 0f;
				PlayAudio("sound_medicine_machine_1");
			}
		}

		public void SetNumber(int val, Transform numTrans)
		{
			num = Mathf.Clamp(val, 0, 999);
			string[] array = new string[3];
			if (num < 10)
			{
				array[0] = "0";
				array[1] = "0";
				array[2] = num.ToString();
			}
			else if (num < 100)
			{
				array[0] = "0";
				array[1] = num.ToString().Substring(0, 1);
				array[2] = num.ToString().Substring(1, 1);
			}
			else
			{
				array[0] = num.ToString().Substring(0, 1);
				array[1] = num.ToString().Substring(1, 1);
				array[2] = num.ToString().Substring(2, 1);
			}
			if (numRenders == null)
			{
				numRenders = new SpriteRenderer[3];
				numRenders[0] = numTrans.Find("0").GetComponent<SpriteRenderer>();
				numRenders[1] = numTrans.Find("1").GetComponent<SpriteRenderer>();
				numRenders[2] = numTrans.Find("2").GetComponent<SpriteRenderer>();
			}
			for (int i = 0; i < array.Length; i++)
			{
				string text = array[i];
				numRenders[i].sprite = spriteAssets.GetSprite(text);
			}
		}

		public override void PackData(JSONObject obj)
		{
			base.PackData(obj);
			obj["33"] = colorIndex;
		}

		private float GetRandomTime()
		{
			return Random.Range(0.2f, 0.5f);
		}
	}
}
